package com.ruoyi.project.search.viewStockDetail.controller;

import java.util.List;

import com.ruoyi.project.search.viewStockDetail.domain.ViewStockDetail;
import com.ruoyi.project.search.viewStockDetail.service.IViewStockDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存明细Controller
 * 
 * @author ruoyi
 * @date 2023-06-02
 */
@Controller
@RequestMapping("/search/viewStockDetail")
public class ViewStockDetailController extends BaseController
{
    private String prefix = "search";

    @Autowired
    private IViewStockDetailService viewStockDetailService;

    @GetMapping()
    public String viewStockDetail()
    {
        return prefix + "/viewStockDetail";
    }

    /**
     * 查询库存明细列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ViewStockDetail viewStockDetail)
    {
        startPage();
        List<ViewStockDetail> list = viewStockDetailService.selectViewStockDetailList(viewStockDetail);
        return getDataTable(list);
    }

    /**
     * 导出库存明细列表
     */
    @Log(title = "库存明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ViewStockDetail viewStockDetail)
    {
        List<ViewStockDetail> list = viewStockDetailService.selectViewStockDetailList(viewStockDetail);
        ExcelUtil<ViewStockDetail> util = new ExcelUtil<ViewStockDetail>(ViewStockDetail.class);
        return util.exportExcel(list, "库存明细数据");
    }

    /**
     * 新增库存明细
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存明细
     */
    @Log(title = "库存明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ViewStockDetail viewStockDetail)
    {
        return toAjax(viewStockDetailService.insertViewStockDetail(viewStockDetail));
    }

    /**
     * 修改库存明细
     */
    @GetMapping("/edit/{stockId}")
    public String edit(@PathVariable("stockId") String stockId, ModelMap mmap)
    {
        ViewStockDetail viewStockDetail = viewStockDetailService.selectViewStockDetailByStockId(stockId);
        mmap.put("viewStockDetail", viewStockDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存明细
     */
    @Log(title = "库存明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ViewStockDetail viewStockDetail)
    {
        return toAjax(viewStockDetailService.updateViewStockDetail(viewStockDetail));
    }

    /**
     * 删除库存明细
     */
    @Log(title = "库存明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(viewStockDetailService.deleteViewStockDetailByStockIds(ids));
    }

    /**
     * 指定货位出库
     * @param stockId  库存编号
     * @return
     */
    @Log(title = "指定货位出库", businessType = BusinessType.OTHER)
    @RequestMapping("/assignOut")
    @ResponseBody
    public AjaxResult assignOut(String stockId){
        int resultCode = viewStockDetailService.assignOut(stockId);
        if (resultCode == 1){
            return AjaxResult.error("无该库存，请检查！");
        } else if (resultCode == 2){
            return AjaxResult.error("该条码已有作业！");
        } else if (resultCode == 3){
            return AjaxResult.error("货位状态有误，请检查！");
        } else if (resultCode == 4){
            return AjaxResult.error("货位被锁定，请解锁后重试！");
        } else if (resultCode == 10){
            return AjaxResult.success("操作成功！");
        } else {
            return AjaxResult.error("未知错误，请联系管理员！");
        }
    }
}
